package com.jww.service.impl;

import com.github.pagehelper.PageHelper;
import com.jww.domain.store.Catalog;
import com.jww.domain.store.Course;
import com.jww.mapper.store.CatalogMapper;
import com.jww.mapper.store.CompanyServiceMapper;
import com.jww.mapper.store.CourseMapper;
import com.jww.service.CatalogService;
import com.jww.utils.MapperUtil;
import com.jww.utils.MyUtils;
import com.jww.utils.TransactionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class CatalogServiceImpl implements CatalogService {
    @Override
    public void save(Catalog catalog) {
        SqlSession sqlSession = null;

        sqlSession = MyUtils.getSqlSession();
        CatalogMapper mapper = sqlSession.getMapper(CatalogMapper.class);

        //主键id处理
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        catalog.setId(id);

        mapper.save(catalog);
        MyUtils.close(sqlSession);

    }

    @Override
    public List<Catalog> findAll(String pageNum, String pageSize) {
        SqlSession sqlSession = null;

        try {
            sqlSession = MyUtils.getSqlSession();
            CatalogMapper mapper = sqlSession.getMapper(CatalogMapper.class);
            //分页
            //默认当前页为1
            int pageN = 1;
            //默认每页显示条目数为10
            int pageS = 10;
            //对获取的当前页码和每页显示条目数进行类型处理

            if (StringUtils.isNotBlank(pageNum)) {
                pageN = Integer.parseInt(pageNum);
            }

            if (StringUtils.isNotBlank(pageSize)) {
                pageS = Integer.parseInt(pageSize);
            }
            //设置分页参数：当前页码 每页显示的条目数
            PageHelper.startPage(pageN, pageS);

            List<Catalog> list = mapper.findAll();

            return list;
        } catch (Exception e) {
            throw new RuntimeException("查询失败====>>");
        } finally {
            MyUtils.close(sqlSession);
        }
    }

    @Override
    public List<Catalog> findAll() {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MyUtils.getSqlSession();
            //2.获取Mapper
            CatalogMapper catalogMapper = sqlSession.getMapper( CatalogMapper.class);
            //3.调用Mapper层操作
            return catalogMapper.findAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //删除
    @Override
    public boolean deleteByIds(String ids) {
        SqlSession sqlSession = null;
        boolean flag = false;
        try {
            sqlSession = MyUtils.getSqlSession();
            CatalogMapper mapper = sqlSession.getMapper(CatalogMapper.class);

            String[] id = ids.split(",");
            mapper.delete(id);
            flag = true;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyUtils.close(sqlSession);
        }
        return flag;
    }

    @Override
    public Catalog findById(String id) {
        SqlSession sqlSession = null;

        sqlSession = MyUtils.getSqlSession();
        CatalogMapper mapper = sqlSession.getMapper(CatalogMapper.class);

        Catalog catalog = mapper.findById(id);
        MyUtils.close(sqlSession);
        return catalog;

    }

    @Override
    public void update(Catalog catalog) {
        SqlSession sqlSession = null;

        sqlSession = MyUtils.getSqlSession();
        CatalogMapper mapper = sqlSession.getMapper(CatalogMapper.class);

        mapper.update(catalog);
        MyUtils.close(sqlSession);
    }

    @Override
    public List<Catalog> list() {

        SqlSession session = MapperUtil.getSession();
        CatalogMapper mapper = session.getMapper(CatalogMapper.class);
        List<Catalog> list = mapper.findAll();
        MapperUtil.close(session);
        return list;

    }

}
